<?php

namespace app\manage\services;
use app\model\Admin as AdminModel;

class Admin
{
    protected $model;

    public function __construct()
    {
        $this->model = new AdminModel();
    }

    //检查用户是否存在
    public function checkUserLogin($userLogin)
    {
        if(empty($userLogin)){
            return false;
        }
        $where = array('user_login' => trim($userLogin));
        $res = $this->model->where($where)->find();
        if(!$res){
            return false;
        }
        return $res;
    }

    //查询分页数据
    public function getPageList($page, $limit, $where = []): array
    {
        return $this->model->where($where)
            ->order("id desc")
            ->paginate(['page' => $page, 'list_rows' => $limit])
            ->toArray();
    }

    //根据id查询明细
    public function getInfoById($id)
    {
        return $this->model->where('id', $id)->find();
    }

    //新增
    public function add($params)
    {
        return $this->model->createInfo($params);
    }

    //编辑
    public function update($params): bool
    {
        return $this->model->updateInfo($params['id'], $params);
    }

    //删除
    public function delete($id): bool
    {
        $info = $this->model->with('logs')->find($id);
        $res = $info->together(['logs'])->delete();
        if ($res) {
            return true;
        }
        return false;
    }
}